//
//  SEPatientFileBasicViewController.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/17.
//

import UIKit

class SEPatientFileBasicViewController: SEBaseViewController {

    var viewModel = SEPatientFileBasicViewModel()

    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.screenWidth, height: UIScreen.bottomSafeHeight))
            tableView.rowHeight = 50
            tableView.separatorColor = UIColor.init(hex: "EBECF0")
            tableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            tableView.keyboardDismissMode = .onDrag
            tableView.register(UINib(nibName: SEPatientTitleTextFieldCell.className, bundle: nil), forCellReuseIdentifier: SEPatientTitleTextFieldCell.reuseId)
        }
    }

    init(with viewModel: SEPatientDetailViewModel) {
        self.viewModel.patientViewModel = viewModel
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
//        requestForPatientDetail()
    }

    override func setupUI() {
        super.setupUI()
        view.backgroundColor = UIColor.white

        viewModel.updateData = { status in
            switch status {
            case .success:
                self.tableView.isHidden = false
                self.tableView.reloadData()
            case .auth(let msg):
                self.presentLoginViewController(selector: #selector(self.requestForPatientDetail)) {
                    SEHUD.showError(message: msg)
                }
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

    private func tableFooterTextView(placeHolder: String, text: String) -> UIView {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.screenWidth, height: 105))
        view.backgroundColor = UIColor.white
        let textView = SETextView()
        textView.isUserInteractionEnabled = false
        textView.font = UIFont.systemFont(ofSize: 15)
        textView.frame = CGRect(x: 15, y: 0, width: UIScreen.screenWidth - 30, height: 90)
        textView.layer.cornerRadius = 4
        textView.layer.borderWidth = 1
        textView.layer.borderColor = Asset.border.color.cgColor
        textView.layer.masksToBounds = true
        textView.placeholder = placeHolder
        textView.text = text
        view.addSubview(textView)
        return view
    }

}

// MARK: - UITableViewDataSource, UITableViewDelegate

extension SEPatientFileBasicViewController: UITableViewDataSource, UITableViewDelegate {

    // MARK: - UITableViewDataSource

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.dataSource[section].count
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel.dataSource.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SEPatientTitleTextFieldCell.reuseId, for: indexPath) as! SEPatientTitleTextFieldCell
        cell.isUserInteractionEnabled = false
        cell.updateCell(model: viewModel.dataSource[indexPath.section][indexPath.row], indexPath: indexPath)
        return cell
    }

    // MARK: - UITableViewDelegate

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        var placeHolder = ""
        var text = ""
        if section == 0 {
            placeHolder = "未填写"
            text = viewModel.patientViewModel.model.allergyHistory
        }
        if section == 1 {
            placeHolder = "未填写"
            text = viewModel.patientViewModel.model.diagnoseHistory
        }
        return tableFooterTextView(placeHolder: placeHolder, text: text)
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if section == 0 {
            return viewModel.patientViewModel.model.isAllergy == 0 ? 0 : 105
        }
        if section == 1 {
            return 105
        }
        return 0
    }

}

extension SEPatientFileBasicViewController {
    @objc private func requestForPatientDetail() {
        viewModel.requestPatientDetail()
    }
}
